﻿using System;
using System.Windows.Forms;
using Notepad_X.Components;
using Notepad_X.Classes;
namespace Notepad_X
{
    public partial class Favourites : Form
    {
        Main main;
        public Favourites()
        {
            main = Notepad_X.Static.MainInstance;
            InitializeComponent();
        }

        private void list_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (list.SelectedItems.Count > 0)
            {
                l_r.Enabled = true;
            }else{
                l_r.Enabled = false;
            }
        }

        private void l_r_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in list.SelectedItems)
            {
                list.Items.Remove(item);
            }
        }

        private void Favourites_Load(object sender, EventArgs e)
        {
            if (main.IconsCollection.Images.ContainsKey("favourites")){
                this.Icon = System.Drawing.Icon.FromHandle(new System.Drawing.Bitmap(main.IconsCollection.Images["favourites"]).GetHicon());
            }
            this.Text = Main.app_language[Strings.Favourites];
            co_apply.Text = Main.app_language[Strings.Apply];
            co_cancel.Text = Main.app_language[Strings.Cancel];
            l_r.Text = Main.app_language[Strings.Remove];

            foreach (RecentMenuItemX item in Main.app_settings.favourites)
            {
                ListViewItem newitem = new ListViewItem();
                newitem.Text=System.IO.Path.GetFileName(item.path);
                newitem.SubItems.Add(item.path);
                list.Items.Add(newitem);
              
            }
            if (Main.app_settings.icons.ToLower() != "default")
            {
                changeIcons(co_apply, "apply");
                changeIcons(co_cancel, "cancel");
                changeIcons(l_r, "remove");
            }
        }
        private void changeIcons(Button item, string key)
        {
            if ((key != null) && (main.IconsCollection.Images.ContainsKey(key)))
            {
                item.Image = main.IconsCollection.Images[key];
            }
        }
        private void button15_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void co_apply_Click(object sender, EventArgs e)
        {
            Main.app_settings.favourites.Clear();
            foreach (ToolStripItem item in main.system_menuItem_Get("Menu_Tools_Favourites").DropDownItems)
            {
                if(item.GetType()==typeof(RecentMenuItemX)){
                    main.system_menuItem_Get("Menu_Tools_Favourites").DropDownItems.Remove(item);
                    break;
                }
            }
            foreach (ListViewItem item in list.Items)
            {
                Main.app_settings.favourite_add(item.SubItems[1].Text);
            }
            this.Close();
        }
    }
}
